home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk4 / pri / pri.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  3KB  |  73 lines

  1.  
  2. /* PRI, by Ali T. Ozer (ARPA: ali@score.stanford.edu)
  3. ** Written April 1988. Freely distributable.
  4. ** 
  5. ** Usage: pri <pri> <process>
  6. ** 
  7. ** Sets the priority of the specified process to pri. These have to be CLI
  8. ** processes; pri won't change the prioirity of programs started from the WB.
  9. */
  10.  
  11. #include <libraries/dosextens.h>
  12.  
  13. struct DosLibrary *DosBase, *OpenLibrary();
  14. struct FileHandle *Output();
  15.  
  16. #define ILLEGALNUM (-9999)  
  17.  
  18. void Bye (msg) char *msg; {
  19.   struct FileHandle *out;
  20.   if (msg && (out = Output())) Write (out, msg, (long)strlen(msg));
  21.   if (DosBase) CloseLibrary (DosBase);
  22.   exit (msg ? 5 : 0);
  23. }
  24.  
  25. /* Parse a signed integer. Return ILLEGALNUM on parse error.
  26. */
  27. int myatoi (str) char *str; {
  28.   int sign = 1, num = 0;
  29.   if (*str == '-') {str++; sign = -1;} else if (*str == '+') str++;
  30.   while (*str) 
  31.     if (*str < '0' || *str > '9') return (ILLEGALNUM);
  32.     else num = num * 10 - '0' + (*str++);
  33.   return (sign * num);
  34. }
  35.  
  36. main (argc, argv) int argc; char **argv;
  37. {
  38.   int prn, pri;  /* Process number and new desired priority */
  39.   struct RootNode *rn;
  40.   long *lptr;
  41.  
  42.   if (argc == 0) Bye (NULL);  /* Don't run from Workbench */
  43.  
  44.   if (((DosBase = OpenLibrary ("dos.library", 0L)) == NULL) ||
  45.       ((rn = (struct RootNode *)DosBase->dl_Root) == NULL) ||
  46.       ((lptr = (long *)((rn->rn_TaskArray) << 2L)) == NULL)) Bye ("DOS?\n");
  47.  
  48.   /* Now parse the arguments. We need two arguments, first the priority 
  49.   ** (-128..127), and second, the process number (1..Max# of CLIs allowed).
  50.   */
  51.  
  52.   if (argc != 3) Bye ("Set the priority of CLI process to pri\nUsage: pri <pri> <process>\n");
  53.   if ((pri = myatoi(argv[1])) > 127 || pri < -128) Bye ("Bad priority\n");
  54.   prn = myatoi(argv[2]);
  55.  
  56.   /* At this stage, lptr points to AmigaDOS's array of CLIs. The first element
  57.   ** in this array is the maximum number of CLIs allowed. Then follows this
  58.   ** many pointers, which, if not NULL, point to CLI processes. (Actually,
  59.   ** they do not point to the process. A process is simply an Amiga 
  60.   ** Task structure, followed by an Amiga MsgPort structure, and then
  61.   ** followed other stuff. These pointers point to the MsgPort structure.
  62.   ** Thus, to get to the task structure (so we can SetTaskPri()), we'll need
  63.   ** to subtract the size of the task structure from the MsgPort pointer.)
  64.   */
  65.  
  66.   Forbid();
  67.   if ((prn > 0) && ((long)prn <= *lptr) && *(lptr+prn))
  68.     SetTaskPri ((struct Task *)(*(lptr+prn) - sizeof(struct Task)), (long)pri);
  69.     else Bye ("No such process\n");
  70.   Permit();
  71. }
  72.    
  73.